<?php

namespace app\ru\controller;

use app\common\controller\Frontend;
use think\Db;
class Products extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();

        $param = $this->request->param();
        $lanmu = isset($param['lanmu'])?$param['lanmu']:1;
        $cid = isset($param['cid'])?$param['cid']:null;
        
        $erlist = Db::name('productstype')->where('status',$lanmu)->order('weigh','desc')->order('id','desc')->select();


        $this->lanmu = $lanmu;
        $this->cid = $cid;

        $this->view->assign('lanmu', $lanmu);
        $this->view->assign('cid', $cid);
        $this->view->assign('erlist', $erlist);

    }

    public function index()
    {
        $lanmu = $this->lanmu;
        $cid = $this->cid;
        
        if($lanmu==7){

            if(!$cid){

                $danye15 = Db::name('danye')->where('id',15)->find();
                $tuitype = Db::name('productstype')->where('status',$lanmu)->order('weigh','desc')->order('id','desc')->select();

                //不锈钢知识
                $zhishi = Db::name('newslist')->where('newstype','in',get_ertypeid_by_lanmu(25))->where('shswitch',1)->where('tuiswitch',1)->order('weigh','desc')->order('id','desc')->limit(2)->select();

                $this->view->assign('tuitype', $tuitype);
                $this->view->assign('danye15', $danye15);
                $this->view->assign('zhishi', $zhishi);
                return $this->view->fetch();
            }else{

                $show['typeshow'] = Db::name('productstype')->where('id',$cid)->find();
                $list = Db::name('productslist')->where('productstype',$cid)->where('shswitch',1)->order('weigh','desc')->order('id','desc')->select();
                $this->view->assign('show', $show);
                $this->view->assign('list', $list);
                return $this->view->fetch('productstypeshow');
            }

        }else{
            $list = Db::name('productslist')->where('productstype',$cid)->where('shswitch',1)->order('weigh','desc')->order('id','desc')->paginate(6);
            $this->view->assign('list', $list);
        }
        
        
        
    }

    public function show()
    {
        $param = $this->request->param();
        $cid = $this->cid;
        $id = isset($param['id'])?$param['id']:null;

        $show = Db::name('productslist')->where('id',$id)->find();
        $show['typename'] = Db::name('productstype')->where('id',$cid)->value('name');

        $show['prev'] = Db::name('productslist')->where('weigh','>=',$show['weigh'])->where('id','neq',$show['id'])->where('productstype',$cid)->where('shswitch',1)->order('weigh','asc')->order('id','asc')->find();
        
        $show['next'] = Db::name('productslist')->where('weigh','<=',$show['weigh'])->where('id','neq',$show['id'])->where('productstype',$cid)->where('shswitch',1)->order('weigh','desc')->order('id','desc')->find();

        $this->view->assign('show', $show);
        return $this->view->fetch('show');

    }

    public function productstypeshow(){
        return $this->view->fetch('productstypeshow');
    }

}
